<?php

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'listday';
$tabs=array(
	array('op'=>'listday','title'=>'每日销售统计'),
	array('op'=>'listmonth','title'=>'每月销售统计'),
);
if($_RQ['op']=='listday'){
	$params=array('ispay'=>1,'uniacid'=>$_SESSION['uniacid']);
	$y=$_RQ['y']?:date('Y');
	$m=$_RQ['m']?:date('m');
	$return['params']['y']=$y;
	$return['params']['m']=$m;
	$daynum=date("t",strtotime($y.'-'.$m));
	for($i=1;$i<=$daynum;$i++){
		$starttime=strtotime($y.'-'.$m.'-'.$i);
		$endtime=$starttime+24*3600;
		$params['paytime between']=array($starttime,$endtime);
		$fee=pdo_getsum('jy_weishop_order',$params,'price');
		$num=pdo_count('jy_weishop_order',$params);
		$ordernum[]=$num;
		$orderfee[]=$fee;
		$days[] = $i;
	}
	//$ordernum=array_reverse($ordernum);
	//$orderfee=array_reverse($orderfee);
	//$days=array_reverse($days);
	$htmls=array(
		'type'=>'look',
		'tabs'=>$tabs,
		'look'=>array(
			array('type'=>'echarts_arealine','ecid'=>'mobile','title'=>'每日销售统计','datas'=>array(
				'datas'=>array(
					array('name'=>'订单数(笔)(已支付)','value'=>$ordernum),
					array('name'=>'销售额(元)(已支付)','value'=>$orderfee),
				),
				'xdatas'=>$days,
			)),
		),
		'search'=>array(
			array('name'=>'y','width'=>15,'title'=>'年份','type'=>'radio','options'=>'ys'),
			array('name'=>'m','width'=>15,'title'=>'月份','type'=>'radio','options'=>'months'),
		)
	);
	$return['item']=$item;
	$return['htmls']=$htmls;
	$nowy=date('Y');
	$y1=$nowy-2;
	$y2=$nowy-1;
	$return['ys']=array(
		$y1=>$y1.'年',
		$y2=>$y2.'年',
		$nowy=>$nowy.'年',
	);
	$return['months']=array(
		'01'=>'1月',
		'02'=>'2月',
		'03'=>'3月',
		'04'=>'4月',
		'05'=>'5月',
		'06'=>'6月',
		'07'=>'7月',
		'08'=>'8月',
		'09'=>'9月',
		'10'=>'10月',
		'11'=>'11月',
		'12'=>'12月',
	);
	exi($return);
}
if($_RQ['op']=='listmonth'){
	$params=array('ispay'=>1,'uniacid'=>$_SESSION['uniacid']);
	$y=$_RQ['y']?:date('Y');
	if(!empty($_RQ['y'])){
		$return['params']['y']=$_RQ['y'];
	}
	$list=array();
	for($i=1;$i<=12;$i++){
		$starttime=strtotime($y.'-'.$i);
		if($i==12){
			$ny=$y+1;
			$endtime=strtotime($ny.'-01');
		}else{
			$m=$i+1;
			$endtime=strtotime($y.'-'.$m);
		}
		$params['paytime between']=array($starttime,$endtime);
		$fee=pdo_getsum('jy_weishop_order',$params,'price');
		$num=pdo_count('jy_weishop_order',$params);
		//同比增长
		//去年同月统计周期数据
		$y2=$y-1;
		$starttime=strtotime($y2.'-'.$i);
		if($i==12){
			$ny=$y2+1;
			$endtime=strtotime($ny.'-01');
		}else{
			$m=$i+1;
			$endtime=strtotime($y2.'-'.$m);
		}
		$params['paytime between']=array($starttime,$endtime);
		$lastfee=pdo_getsum('jy_weishop_order',$params,'price');
		if($lastfee>0 && $fee>0){
			$rate1=($fee-$lastfee)*100/$lastfee;
			$rate1=price($rate1);
		}else{
			$rate1=0;
		}
		//环比增长
		if($i==1){
			$ny=$y-1;
			$starttime=strtotime($ny.'-12');
		}else{
			$m=$i-1;
			$starttime=strtotime($y.'-'.$m);
		}
		$endtime=strtotime($y.'-'.$i);
		$params['paytime between']=array($starttime,$endtime);
		$lastfee=pdo_getsum('jy_weishop_order',$params,'price');
		if($lastfee>0 && $fee>0){
			$rate2=($fee-$lastfee)*100/$lastfee;
			$rate2=price($rate2);
		}else{
			$rate2=0;
		}
		$ordernum[]=$num;
		$orderfee[]=$fee;
		$orderrate1[]=$rate1;
		$orderrate2[]=$rate2;
		$days[] = $i;
	}
	$htmls=array(
		'type'=>'look',
		'tabs'=>$tabs,
		'look'=>array(
			array('type'=>'echarts_arealine','ecid'=>'mobile','title'=>'每日销售统计','datas'=>array(
				'datas'=>array(
					array('name'=>'订单数(笔)(已支付)','value'=>$ordernum),
					array('name'=>'销售额(元)(已支付)','value'=>$orderfee),
					array('name'=>'同比增长','value'=>$orderrate1),
					array('name'=>'环比增长','value'=>$orderrate2),
				),
				'xdatas'=>$days,
			)),
		),
		'search'=>array(
			array('name'=>'y','width'=>15,'title'=>'年份','type'=>'radio','options'=>'ys'),
		)
	);
	$return['htmls']=$htmls;
	$nowy=date('Y');
	$y1=$nowy-2;
	$y2=$nowy-1;
	$return['ys']=array(
		$y1=>$y1.'年',
		$y2=>$y2.'年',
		$nowy=>$nowy.'年',
	);
	exi($return);
}